﻿#region (C) Header Region @ 2012
//====================================================
// 文件: LogManager.cs
// 描述: 统一系统日志管理器
// 路径: Antu.Core\Logger\LogManager.cs
// ******************版本1 ******************
// 人员: 蔡金保    时间 : 2012/11/15 11:25:43
// 注释:
//====================================================
#endregion

using System;
using System.Data;
using System.Configuration;
using System.Configuration.Provider;
using System.Web.Configuration;
using System.Collections.Specialized;
using System.Collections;
using System.Text;
using System.Reflection;
using Antu.Core.Services;

namespace Antu.Core.Logger
{
    /// <summary>
    /// 日志管理器
    /// </summary>
    public class LogManager
    {
        private LogProviderBase _provider;
        private LogProviderCollection _providers;
        private bool _useErrLog;
        private bool _useInfoLog;

        static LogManager defaultLogManager = new LogManager();
        /// <summary>
        /// 获取指定的日志提供者
        /// </summary>
        public static LogManager Manager
        {
            get
            {
                return defaultLogManager;
            }
        }

        private LogManager()
        {
            InitLogManage();
        }

        /// <summary>
        /// 初始化配置文件 注：当配置文件被更改后要触发该函数已实现更换配置信息
        /// </summary>
        public void InitLogManage()
        {
            ConfigSectionListenterService _ConfigSectionListenterService = (ConfigSectionListenterService)ServiceManager.Manager.GetService(typeof(ConfigSectionListenterService));
            LogProviderConfigurationSection section = _ConfigSectionListenterService.GetSection<LogProviderConfigurationSection>("logProvider");
            _providers = new LogProviderCollection();
            ProvidersHelper.InstantiateProviders(section.Providers, _providers, typeof(LogProviderBase));
            _provider = _providers[section.DefaultProvider];

            _useErrLog = Convert.ToBoolean(section.UseErrLog);
            _useInfoLog = Convert.ToBoolean(section.UseInfoLog);

            if (_provider == null)
                throw new ProviderException("不能获取默认的 LogProvider");
        }

        /// <summary>
        /// 写入异常日志
        /// </summary>
        /// <param name="e">异常</param>
        /// <param name="parames">异常参数</param>
        /// <returns>成功为True 否则为False</returns>
        public bool OutputErrorLog(Exception e)
        {
            return OutputErrorLog(e, new object[] { e.TargetSite, e.Source });
        }
        /// <summary>
        /// 写入异常日志
        /// </summary>
        /// <param name="e">异常</param>
        /// <param name="parames">异常参数</param>
        /// <returns>成功为True 否则为False</returns>
        public bool OutputErrorLog(Exception e, Object[] parames)
        {
            if (_useErrLog)
            {
                MethodInfo _method = (MethodInfo)(new System.Diagnostics.StackTrace().GetFrame(1).GetMethod());//从堆栈中获得异常方法
                Hashtable _parameters = new Hashtable();
                //将参数信息放入一个哈希表中
                if (parames != null && _method.GetParameters() != null)
                {
                    foreach (ParameterInfo pi in _method.GetParameters())
                    {
                        if (parames.Length <= pi.Position)
                            _parameters.Add(pi.Name, "");
                        else
                            _parameters.Add(pi.Name, parames[pi.Position]);
                    }
                }
                string parameList = "[";
                if (_parameters != null)
                {
                    foreach (DictionaryEntry de in _parameters)
                    {
                        if (de.Value != null)
                            parameList += de.Key.ToString() + "=" + de.Value.ToString() + ";";
                    }
                }
                parameList += "]";
                return _provider.OutputErrorLog(_method.Name, parameList, e);
            }
            else
                return false;
        }
        /// <summary>
        /// 写入异常日志
        /// </summary>
        /// <param name="e">异常</param>
        /// <param name="parames">异常参数</param>
        /// <returns>成功为True 否则为False</returns>
        public bool OutputInfoLog(string msg, Object[] parames)
        {
            if (_useInfoLog)
            {
                MethodInfo _method = (MethodInfo)(new System.Diagnostics.StackTrace().GetFrame(1).GetMethod());//从堆栈中获得异常方法
                Hashtable _parameters = new Hashtable();
                //将参数信息放入一个哈希表中
                if (parames != null && _method.GetParameters() != null)
                {
                    foreach (ParameterInfo pi in _method.GetParameters())
                    {
                        if (parames.Length <= pi.Position)
                            _parameters.Add(pi.Name, "");
                        else
                            _parameters.Add(pi.Name, parames[pi.Position]);
                    }
                }
                string parameList = "[";
                if (_parameters != null)
                {
                    foreach (DictionaryEntry de in _parameters)
                    {
                        if (de.Value != null)
                            parameList += de.Key.ToString() + "=" + de.Value.ToString() + ";";
                    }
                }
                parameList += "]";
                return _provider.OutputInfoLog(_method.Name, parameList, msg);
            }
            else
                return false;
        }
        public bool OutputInfoLog(string msg, Object[] parames,  string fileext)
        {
            if (_useInfoLog)
            {
                MethodInfo _method = (MethodInfo)(new System.Diagnostics.StackTrace().GetFrame(1).GetMethod());//从堆栈中获得异常方法
                Hashtable _parameters = new Hashtable();
                //将参数信息放入一个哈希表中
                if (parames != null && _method.GetParameters() != null)
                {
                    foreach (ParameterInfo pi in _method.GetParameters())
                    {
                        if (parames.Length <= pi.Position)
                            _parameters.Add(pi.Name, "");
                        else
                            _parameters.Add(pi.Name, parames[pi.Position]);
                    }
                }
                string parameList = "[";
                if (_parameters != null)
                {
                    foreach (DictionaryEntry de in _parameters)
                    {
                        if (de.Value != null)
                            parameList += de.Key.ToString() + "=" + de.Value.ToString() + ";";
                    }
                }
                parameList += "]";

                return _provider.OutputInfoLog(_method.Name, parameList, msg,  fileext);
            }
            else
                return false;
        }

    }
}
